package com.xyrl.project.app.core.config;

import com.xyrl.common.core.properties.SwaggerProperties;
import com.xyrl.common.core.properties.XyrlProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@EnableSwagger2
@Configuration
public class Swagger2Config {
    @Autowired
    private XyrlProperties properties;

    @Bean
    public Docket swaggerApiCommon() {
        SwaggerProperties swagger = properties.getSwagger();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("公共模块")
                .enable(swagger.isOpen())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage() + ".common"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo(swagger));

    }
//    b9752a7c24e143196871a26491159bee4a42bcc8fd2a155dedfa8077557adb77ebbdd7894da29907bd54b3261c0ce2173400b238d73faf8cc1bffae1861dd0d89dda33fc8c9738688d0bb7d11bc86a5f02bd6288cfab9ad83f9bc260aa28fd66f3ca26d9988480148b2ae6fa3a03122ef0e243068b6b708ab61623a9f85b61498ed8256cbb2a637eda29db9c1f463f16c6eb2caf741072501da464813daf60aba364985f964568cc54783d1867790f480adfd55116dc13124dc848ba61e5872de7b4e3d646b6e85d0c42bb916457233a438ea50d156db12d7e9b9c83ebdf51a8
    private List<ApiKey> security() {
        List<ApiKey> list = new ArrayList();
        ApiKey apiKey = new ApiKey("token", "token", "header");
        list.add(apiKey);
        return list;
    }
    @Bean
    public Docket swaggerApiSalesman() {
        SwaggerProperties swagger = properties.getSwagger();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("业务员APP")
                .enable(swagger.isOpen())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage() + ".sales"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo(swagger))
                .securitySchemes(security());
    }

    @Bean
    public Docket swaggerApiCustomer() {
        SwaggerProperties swagger = properties.getSwagger();
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("商户APP")
                .enable(swagger.isOpen())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage() + ".csr"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo(swagger));
    }

    private ApiInfo apiInfo(SwaggerProperties swagger) {
        return new ApiInfo(
                swagger.getTitle(),
                swagger.getDescription(),
                swagger.getVersion(),
                null,
                new Contact(swagger.getAuthor(), swagger.getUrl(), swagger.getEmail()),
                swagger.getLicense(), swagger.getLicenseUrl(), Collections.emptyList());
    }
}
